/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.entity;

import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

@Entity
@XmlAccessorType(XmlAccessType.NONE)
@NamedQueries({
    @NamedQuery(name = "Participation.findByUser", query = "SELECT p FROM Participation p WHERE p.user = :user AND p.state<>:state"),
    @NamedQuery(name = "Participation.findByEvent", query = "SELECT p FROM Participation p WHERE p.event = :event"),
    @NamedQuery(name = "Participation.findByEventUser", query = "SELECT p FROM Participation p WHERE p.event = :event AND p.user= :user "),
    @NamedQuery(name = "Participation.findByEventNotRemoved", query = "SELECT p FROM Participation p WHERE p.event = :event AND p.removed=false"),

})
public class Participation implements Serializable {
   
    private static final long serialVersionUID = 1L;
    public static final String findByUser = "Participation.findByUser";
    public static final String findByEvent = "Participation.findByEvent";
    public static final String findByEventUser = "Participation.findByEventUser";
    public static final String findByEventNotRemoved = "Participation.findByEventNotRemoved";
    
    @XmlElement()
    @Id
    @OneToOne
    private Users user;
    
    @XmlElement()
    @Id
    @OneToOne() //xml recovery
    private Event event;
    
    public Participation(){
        
    }
    
    public Participation(Users user, Event event){
        this.user=user;
        this.event=event;
        this.state=state.PENDING;
        this.removed=false;
    }
    
    public Participation(Users user, Event event, State state){
        this.user=user;
        this.event=event;
        this.state=state;
    }
    
    public enum State{
        PENDING,
        REFUSED,
        CONFIRMED
    }
    
    
    @XmlElement()
    @Enumerated(EnumType.STRING)
    private State state;
    
    @XmlAttribute()
    private boolean removed;
    
    public void setUser(Users user){
        this.user=user;
    }
    
    public Users getUser(){
        return this.user;
    }
    
    public void setEvent(Event event){
        this.event=event;
    }

    public Event getEvent(){
        return this.event;
    }
    
    public void setState(Participation.State state){
        this.state=state;
    }
    
    public State getState(){
        return this.state;
    }
    
    public boolean isRemoved(){
        return this.removed;
    }
    
    public void setRemoved(boolean removed){
        this.removed=removed;
    }
  
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Participation other = (Participation) obj;
        return Objects.equals(this.event, other.event) && Objects.equals(this.user, other.user);
    }
}
